﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace CVSS.Client.BDM.Business
{
    public class LimitedSpeedHelper
    {
        public LimitedSpeedHelper()
        {

        }

        public LimitedSpeedHelper(AxMapControl mapControl)
        {
            m_mapControl = mapControl;
        }

        private IFeatureClass[] m_roadFCs = null;
        private AxMapControl m_mapControl = null;

        /// <summary>
        /// 地图控件
        /// </summary>
        public AxMapControl MAPCONTROL
        {
            get { return m_mapControl; }
            set { m_mapControl = value; }
        }

        /// <summary>
        /// 描述：修改道路信息
        /// </summary>
        /// <returns></returns>
        public bool ModifyRoadInfo()
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用ModifyRoadInfo函数");

                //根据图层名获取图层
                string lyr_name = "";
                IFeatureClass featCls = GetRoadFeatureClass(lyr_name);

                //根据OID获取对应的Feature
                int oid = 0;
                IFeature feat = featCls.GetFeature(oid);

                //修改限速值
                int index = 0;
                string speed_field = "Speed";
                int speed_value = 80;
                index = feat.Fields.FindField(speed_field);
                if (index >= 0)
                    feat.set_Value(index, speed_value);

                //修改起点里程
                string start_mileage = "Begin_Map";
                double startMValue = 0;
                index = feat.Fields.FindField(start_mileage);
                if (index >= 0 && startMValue != Convert.ToDouble(feat.get_Value(index)))
                {
                    /*
                     * 修改相应的起点里程
                     * 
                    double oStartValue = Convert.ToDouble(feat.get_Value(index));
                    double startre = startMValue - oStartValue;
                    bool boolre = ModifyMileage(featCls, feat.OID + 1, startre, index);
                     * 
                     * */

                    feat.set_Value(index, startMValue);
                }

                //修改终点里程
                string end_mileage = "End_Map";
                double endMValue = 0;
                index = feat.Fields.FindField(end_mileage);
                if (index >= 0 && endMValue != Convert.ToDouble(feat.get_Value(index)))
                {
                    /*
                     * 修改相应的终点里程
                     * 
                    double oEndValue = Convert.ToDouble(feat.get_Value(index));
                    double endre = endMValue - oEndValue;
                    bool boolre = ModifyMileage(featCls, feat.OID + 1, endre, index);
                     * 
                    */

                    feat.set_Value(index, endMValue);
                }

                //保存修改
                feat.Store();

                SysCommon.Global.mainLog.Debug("调用ModifyRoadInfo函数结束");

                return true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ModifyRoadInfo函数出错", ex);
            }

            return false;
        }

        /// <summary>
        /// 描述：类调用前的初始化
        /// </summary>
        public void Initial()
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用Initial函数");

                m_roadFCs = GetRoadFeatureClass();

                SysCommon.Global.mainLog.Debug("调用Initial函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Initial函数出错", ex);
            }

        }

        /// <summary>
        /// 描述：类调用前的初始化
        /// </summary>
        /// <param name="mapControl">地图控件</param>
        public void Initial(AxMapControl mapControl)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用Initial函数");

                m_mapControl = mapControl;
                m_roadFCs = GetRoadFeatureClass();

                SysCommon.Global.mainLog.Debug("调用Initial函数结束");
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Initial函数出错", ex);
            }
        }

        private IFeatureClass GetRoadFeatureClass(string fc_name)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用GetRoadFeatureClass函数");

                IFeatureClass featclass = null;
                foreach (IFeatureClass fc in m_roadFCs)
                {
                    if (fc.AliasName == fc_name.Trim())
                    {
                        featclass = fc;
                        break;
                    }
                }

                SysCommon.Global.mainLog.Debug("调用GetRoadFeatureClass函数结束");
                return featclass;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用GetRoadFeatureClass函数出错", ex);
                return null;
            }

        }

        private IFeatureClass[] GetRoadFeatureClass()
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用GetRoadFeatureClass函数");

                string database_path = "";
                IWorkspaceFactory wsf = new AccessWorkspaceFactoryClass();
                IFeatureWorkspace fws = (IFeatureWorkspace)wsf.OpenFromFile(database_path, 0);

                int lyrCount = m_mapControl.LayerCount;
                IFeatureLayer flyr = null;
                IFeatureClass fc = null;
                //
                IFeatureClass[] roadFCs = null;
                ArrayList fc_names = new ArrayList();

                for (int i = 0; i < lyrCount; i++)
                {
                    flyr = (IFeatureLayer)m_mapControl.get_Layer(i);
                    fc = flyr.FeatureClass;
                    if (fc.GetFeature(1).Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                        fc_names.Add(fc.AliasName);
                }

                int fcCount = fc_names.Count;
                roadFCs = new IFeatureClass[fcCount];
                for (int j = 0; j < fcCount; j++)
                {
                    roadFCs[j] = fws.OpenFeatureClass(fc_names[j].ToString());
                }

                SysCommon.Global.mainLog.Debug("调用GetRoadFeatureClass函数结束");

                return roadFCs;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用GetRoadFeatureClass函数出错", ex);
                return null;
            }
        }

        private bool ModifyMileage(IFeatureClass featCls, int oid, double mileage, int index)
        {
            try
            {
                SysCommon.Global.mainLog.Debug("开始调用ModifyMileage函数");

                if (index < 0) return false;

                //获取要修改的Feature
                IFeature feat = featCls.GetFeature(oid);

                //原始值
                double oMileage = Convert.ToDouble(feat.get_Value(index));
                double result = oMileage + mileage;
                if (result > 0)
                {
                    feat.set_Value(index, result);
                }
                else
                {
                    feat.Delete();
                }

                feat.Store();

                SysCommon.Global.mainLog.Debug("调用ModifyMileage函数结束");

                return true;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ModifyMileage函数出错", ex);
                return false;
            }
        }
    }

}
